﻿namespace WebUI.Controllers
{
    using System;
    using System.Web.Mvc;

    using Kanban.Dto;
    using Kanban.Dto.AutoMapper;

    using KanbanBoard.Core;
    using KanbanBoard.Core.DataInterfaces;

    using MvcContrib.Pagination;

    using SharpArch.Web.NHibernate;

    [HandleError]
    public class UserController : Controller
    {

        private IUserRepository userRepository;

        public UserController(IUserRepository userRepository)
        {
            this.userRepository = userRepository;
        }

        [Transaction]
        [AutoMap(typeof(User), typeof(UserInput))]
        public ActionResult Index(int? page)
        {
            var users = userRepository.GetAll().AsPagination(page ??1, 20);
            return View(users);
        }

        public ActionResult Show(int id)
        {
            var user = userRepository.Get(id);
            return this.View(user);
        }

        public ActionResult Create()
        {
            return this.View();
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [HttpPost]
        public ActionResult Create(User user)
        {
            if (ViewData.ModelState.IsValid && user.IsValid())
            {
                userRepository.SaveOrUpdate(user);

                TempData[GlobalEnums.TempDataProperty.PageMessage.ToString()] = "The user was successfully created.";
                return RedirectToAction("Index");
            }

            return this.View(user);

        }

        [Transaction]
        [AutoMap(typeof(User), typeof(UserInput))]
        public ActionResult Edit(int id)
        {
            var user = userRepository.Get(id);
            return View(user);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [HttpPost]
        public ActionResult Edit(UserInput user)
        {
            var toUpdate = userRepository.Get(user.Id);
            
            if (ViewData.ModelState.IsValid)
            {
                UpdateModel(toUpdate, new [] {"LoginName", "Password", "FirstName", "Surname", "Gender" });
                userRepository.SaveOrUpdate(toUpdate);

                try
                {
                    
                    userRepository.DbContext.CommitChanges();
                    TempData[GlobalEnums.TempDataProperty.PageMessage.ToString()] =
                    "The user was successfully updated.";
                    return RedirectToAction("Index");
                }
                catch 
                {
                    userRepository.DbContext.RollbackTransaction();
                    TempData[GlobalEnums.TempDataProperty.PageMessage.ToString()] =
                    "An error handled.";
                    return this.View(user);
                }
            }
            TempData[GlobalEnums.TempDataProperty.PageMessage.ToString()] = "Input values are not valid";

            return this.View(user);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [HttpPost]
        public ActionResult Delete(int id)
        {
            var resultMessage = "The user was successfully deleted.";
            var toDelete = userRepository.Get(id);

            if (toDelete != null)
            {
                userRepository.Delete(toDelete);

                try
                {
                    userRepository.DbContext.CommitChanges();
                }
                catch
                {
                    resultMessage = "A problem was encountered preventing the user from being deleted. " +
                        "Another item likely depends on this client.";
                    userRepository.DbContext.RollbackTransaction();
                }
            }
            else
            {
                resultMessage = "The user could not be found for deletion. It may already have been deleted.";
            }

            TempData[GlobalEnums.TempDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }
        
    }
}
